package com.dzt.androidframework.network.mobapi.cook;

import android.content.Intent;

import com.dzt.androidframework.network.MvpPresenter;
import com.dzt.androidframework.network.MvpView;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by M02323 on 2017/12/7.
 */

public class CookMenuPresenter implements MvpPresenter {
	private CookManager manager;
	private CompositeSubscription compositeSubscription;
	private CookMenuView cookMenuView;
	private CookMenu menu;

	@Override
	public void onCreate() {
		manager = new CookManager();
		compositeSubscription = new CompositeSubscription();
	}

	@Override
	public void onStart() {

	}

	@Override
	public void onStop() {

	}

	@Override
	public void onDestroy() {
		if (compositeSubscription.hasSubscriptions()) {
			compositeSubscription.unsubscribe();
		}
	}

	@Override
	public void attachView(MvpView view) {
		cookMenuView = (CookMenuView) view;
	}

	@Override
	public void attachIncomingIntent(Intent intent) {

	}

	public void queryCookMenu(String cid, int page, int size) {
		compositeSubscription.add(manager.queryCookMenu(CookService.KEY, cid, page, size)
				.subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Observer<CookMenu>() {
					@Override
					public void onCompleted() {
						if (menu != null) {
							cookMenuView.onSuccess(menu);
						}
					}

					@Override
					public void onError(Throwable e) {
						cookMenuView.onError("请求失败");
					}

					@Override
					public void onNext(CookMenu menu) {
						CookMenuPresenter.this.menu = menu;
					}
				}));
	}
}
